package com.nuoniu.sibanyun.controller.system;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.entity.SysMenu;
import com.nuoniu.sibanyun.entity.dto.MenuSaveDto;
import com.nuoniu.sibanyun.entity.dto.MenuSearchDto;
import com.nuoniu.sibanyun.service.ISysMenuService;
import com.nuoniu.sibanyun.common.result.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/sysMenu")
@Api(value="菜单",tags={"系统管理"})
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService sysMenuService;

    /*************************     系统管理用户菜单相关API  ************************************/
    @GetMapping("/system/page")
//    @PreAuthorize("hasAuthority('sys:menu:list') and authentication.principal == 'system' ")
    @ApiOperation(value="分页查询菜单",notes="分页查询菜单")
    @PreAuthorize("'system' == authentication.principal ")
//    @PreAuthorize("hasRole('SYSTEM_ADMIN') and  authentication.principal == 'system'")
    public HttpResult list(MenuSearchDto menuDto){
        return HttpResult.ok(sysMenuService.pageMenus(menuDto));
    }

    @PreAuthorize("'system' == authentication.principal ")
    @PostMapping("/system/saveSysMenu")
    public HttpResult saveSysMenu(@RequestBody MenuSaveDto menuSaveDto){
        return HttpResult.ok(sysMenuService.saveSysMenu(menuSaveDto,request));
    }

    @DeleteMapping("/system/delete")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult deleteSysMenu(Long id){
        return HttpResult.ok(sysMenuService.deleteSysMenu(id));
    }

    @PostMapping("/system/batchUpdateTreeSorts")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult batchUpdateTreeSorts(@RequestBody List<SysMenu> sysMenuList){
        return HttpResult.ok(sysMenuService.batchUpdateTreeSorts(sysMenuList));
    }






    /*************************     普通用户菜单相关API  ************************************/
    @GetMapping("/user/list")
    @ApiOperation(value = "PC用户权限菜单")
    public HttpResult listRouterMenuVoByUserId(){
        return HttpResult.ok(sysMenuService.listRouterMenuVoByUserId(request));
    }
    @GetMapping("/user/getAppUserModuleVoList")
    @ApiOperation(value = "APP用户权限菜单",tags = "APP_权限")
    public HttpResult getAppUserModuleVoList(){
        return HttpResult.ok(sysMenuService.getAppUserModuleVoList());
    }


    /**
     * 获取管理员菜单权限
     * @author 小啊刚
     * @date 2020/6/12
     */
    @GetMapping("/system/getAdminMenu")
    public HttpResult getAdminMenu(Long id) {return HttpResult.ok(sysMenuService.getAdminMenu(id));}
}
